<?php

namespace App\MoonBundle\Features\Context;

use Behat\Behat\Context\BehatContext;

use App\DefaultBundle\Features\Context\DefaultContext;

use App\MoonBundle\Service\MoonPhaseService;

require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';

/**
 * Features context.
 */
class FeatureContext extends BehatContext
{
    private $date;
    private $time;
    private $age;

    public function __construct()
    {
        $this->useContext('subcontext_default', new DefaultContext());
    }

    /**
     * @Given /^Date is "([^"]*)"$/
     */
    public function dateIs($date)
    {
        $date = explode('.', $date);
        $this->date['day'] = (int)$date[0];
        $this->date['month'] = (int)$date[1];
        $this->date['year'] = (int)$date[2];
    }

    /**
     * @Given /^Time is "([^"]*)"$/
     */
    public function timeIs($time)
    {
        $time = explode(':', $time);

        $this->time['hour'] = (int)$time[0];
        $this->time['minutes'] = (int)$time[1];
        $this->time['second'] = 0;
    }

    /**
     * @When /^I call "([^"]*)"$/
     */
    public function iCall($call)
    {
        $time = mktime($this->time['hour'], $this->time['minutes'], $this->time['second'],
                       $this->date['month'], $this->date['day'], $this->date['year'] );
        $moon = new MoonPhaseService($time);

        $this->age = call_user_func(array($moon, $call));
    }

    /**
     * @Then /^I should have "([^"]*)"$/
     */
    public function iShouldHave($ageGiven)
    {
        if ($ageGiven !== (string)$this->age) {
            throw new \Exception("Error Processing test: " . $this->age);
        }
    }

}
